package com.juli.school.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.juli.school.dao.entity.Dept;
import com.juli.school.dao.entity.School;
import com.juli.school.dao.mapper.DeptMapper;
import com.juli.school.dao.mapper.SchoolMapper;
import com.juli.school.service.bo.DeptBo;
import com.juli.school.service.bo.SchoolBo;
import com.juli.school.service.utils.BeanUtils;
import com.juli.school.utils.core.model.CommonResp;
import com.juli.school.utils.log.annotation.DigestLog;

@Service
public class SchoolService {

	private static final Logger logger = LoggerFactory.getLogger(SchoolService.class);

	@Resource
	private SchoolMapper schoolMapper;
	@Resource
	private DeptMapper deptMapper;

	@DigestLog
	public CommonResp<Boolean> insert(SchoolBo schoolBo) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		if (schoolBo == null) {
			commonResp.fail("学校入参为空");
			return commonResp;
		}
		try {
			schoolMapper.insert(schoolBo);
			commonResp.success();
		} catch (Exception e) {
			logger.error("新增学校发生异常", e);
			commonResp.exception("新增学校发生异常:" + e.getCause());
		}
		return commonResp;
	}

	@DigestLog
	public CommonResp<Boolean> update(SchoolBo schoolBo) {
		CommonResp<Boolean> commonResp = new CommonResp<Boolean>();
		if (schoolBo == null) {
			commonResp.fail("学校入参为空");
			return commonResp;
		}
		try {
			schoolMapper.update(schoolBo);
			commonResp.success();
		} catch (Exception e) {
			logger.error("修改学校信息发生异常", e);
			commonResp.exception("修改学校信息发生异常:" + e.getCause());
		}
		return commonResp;
	}

	@DigestLog
	public CommonResp<SchoolBo> get(Integer id) {
		CommonResp<SchoolBo> commonResp = new CommonResp<SchoolBo>();
		if (id == null) {
			commonResp.fail("学校主键不可为空");
			return commonResp;
		}
		try {
			School school = schoolMapper.getById(id);
			SchoolBo schoolBo = new SchoolBo();
			BeanUtils.copyProperties(school, schoolBo);
			commonResp.setData(schoolBo);
			commonResp.success();
		} catch (Exception e) {
			logger.error("查询学校信息发生异常", e);
			commonResp.exception("查询学校信息发生异常:" + e.getCause());
		}
		return commonResp;
	}

	public CommonResp<List<SchoolBo>> listPage(String prv, String city, String college) {
		CommonResp<List<SchoolBo>> commonResp = new CommonResp<List<SchoolBo>>();
		try {
			if (StringUtils.isNotBlank(college)) {
				commonResp.fail("入参不可为空");
			}
			SchoolBo schoolBo = new SchoolBo();
			if (StringUtils.isNotBlank(prv)) {
				schoolBo.setProv("%" + prv + "%");// 省份
			}
			if (StringUtils.isNotBlank(city)) {
				schoolBo.setCity("%" + city + "%");// 市
			}
			schoolBo.setCollege("%" + college + "%");// 学校名称
			List<School> listPage = schoolMapper.list(schoolBo);
			ArrayList<SchoolBo> data = Lists.newArrayList();
			for (School school : listPage) {
				DeptBo deptBo = new DeptBo();
				deptBo.setCollId(Integer.valueOf(school.getColl_id()));
				List<Dept> list = deptMapper.list(deptBo);
				SchoolBo sb = new SchoolBo();
				BeanUtils.copyProperties(sb, school);
				sb.setDeptList(list);
				data.add(sb);
			}
			commonResp.success();
			commonResp.setData(data);
		} catch (Exception e) {
			logger.error("获取学校列表异常", e);
			commonResp.exception("获取学校列表异常");
		}
		return commonResp;
	}
}
